package com.huitone.smspfm.interfacz.model;

import java.util.ArrayList;
import java.util.List;

import com.bin.common.utils.StringUtils;



public class MtOldSms {
	
	private static final String defaultEncode = "GBK";
	private static final int CONTENT_CH_CHAR_LEN = 64;
	private static final int CONTENT_BIT_LEN = 128;

	//"RecvModuleName","OrgSerialNo","SendPort","User","Smcont","SmType"
	private String clientName;//1
	private String serialNo;//2
	private String sendPort;//3
	private String mobileNo;//4
	private String content;//5
	private String wapUrl;//保留
	private int mesgType;//6
	private int srr;//7
	
	private String srvId;
	
	public static final ArrayList<String> OLD_SMS = new ArrayList<String>() {

		private static final long serialVersionUID = 1L;
		
		{
			add(0,"<clientname>?</clientname>");
			add(1,"<serialno>?</serialno>");
			add(2,"<sendport>?</sendport>");
			add(3,"<mobileno>?</mobileno>");
			add(4,"<content>?</content>");
			add(5,"<mesgtype>?</mesgtype>");
			add(6,"<srr>?</srr>");
		}
		
	};
	
	public MtOldSms(){
		this.setSrr(1);
		this.setWapUrl("");
	}
	private String getValue(int i) {
		String result = null;
		switch (i) {
		case 0:
			result = this.getClientName();
			break;
		case 1:
			result = this.getSerialNo();
			break;
		case 2:
			result = this.getSendPort();
			break;
		case 3:
			result = this.getMobileNo();
			break;
		case 4:
			result = this.getContent();
			break;
		case 5:
			result = StringUtils.toString(this.getMesgType());
			break;
		case 6:
			result =  StringUtils.toString(this.getSrr());
			break;
		default:
			break;
		}
		return result;
	}
	
	
	public String getSrvId() {
		return srvId;
	}
	public void setSrvId(String srvId) {
		this.srvId = srvId;
	}
	public String getClientName() {
		return clientName;
	}
	public void setClientName(String clientName) {
		this.clientName = clientName;
	}
	public String getSerialNo() {
		return serialNo;
	}
	public void setSerialNo(String serialNo) {
		this.serialNo = serialNo;
	}
	public String getSendPort() {
		return sendPort;
	}
	public void setSendPort(String sendPort) {
		this.sendPort = sendPort;
	}
	public String getMobileNo() {
		return mobileNo;
	}
	public void setMobileNo(String mobileNo) {
		this.mobileNo = mobileNo;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getWapUrl() {
		return wapUrl;
	}
	public void setWapUrl(String wapUrl) {
		this.wapUrl = wapUrl;
	}
	public int getMesgType() {
		return mesgType;
	}
	public void setMesgType(int mesgType) {
		this.mesgType = mesgType;
	}
	public int getSrr() {
		return srr;
	}
	public void setSrr(int srr) {
		this.srr = srr;
	}
//	public String toFileString() {
//		StringBuffer sb = new StringBuffer();
//		for(int i = 0; i < OLD_SMS.size(); i++){
//			
//			sb.append(StringUtils.replace(OLD_SMS.get(i),"?",getValue(i)));
//		}
//		
//		return sb.toString();
//	}
	
	private void addAppendAllList(List<StringBuffer> sbs, String msg) {
		for (int i = 0; i < sbs.size(); i++) {
			sbs.get(i).append(msg);
		}
	}
	
	public List<String> toFileStringList(){
		int length = this.getContent().length();
		
		int cnt = length/CONTENT_CH_CHAR_LEN;
		int offer = length % CONTENT_CH_CHAR_LEN;
		if(offer != 0){
			cnt ++;
		}
		List<StringBuffer> sbs =  new ArrayList<StringBuffer>();
		for(int i = 0; i < cnt;i++){
			sbs.add(new StringBuffer());
		}
		for(int i = 0; i < OLD_SMS.size(); i++){
			if(i == 4){// <1>?</1><2>?</2><3>?</3>
				String contentMsg = getValue(i);
				if(cnt ==1) {
					sbs.get(0).append(StringUtils.replace(OLD_SMS.get(i), "?",contentMsg));
				} else {
					for (int j = 0; j < cnt; j++) {
						int start = j * CONTENT_CH_CHAR_LEN;
						int end = start + CONTENT_CH_CHAR_LEN;
						if ((j + 1) == cnt && offer != 0) {
							end = start + offer;
						}
						String baseStr1 = "<1>?</1>";
						String baseStr2 = "<2>?</2>";
						String baseStr3 = "<3>?</3>";
						StringBuffer ssb = new StringBuffer();
						ssb.append(StringUtils.replace(baseStr1, "?", cnt + ""))
								.append(StringUtils.replace(baseStr2, "?",(j + 1) + ""))
								.append(StringUtils.replace(baseStr3, "?",contentMsg.substring(start, end)));
						sbs.get(j).append(StringUtils.replace(OLD_SMS.get(i), "?",ssb.toString()));
					}
				}
			} else {
				addAppendAllList(sbs,(StringUtils.replace(OLD_SMS.get(i),"?",getValue(i))));
			}
		}
		
		addAppendAllList(sbs,"<endl/>");
		List<String> result = new ArrayList<String>();
		for(int i = 0; i < sbs.size();i++){
			result.add(sbs.get(i).toString());
		}
		
		return result;
	}
	
	
	public List<String> toFileStringList2() {
		List<String> contentList = StringUtils.cutStrForByteLenght(this.getContent(), defaultEncode, CONTENT_BIT_LEN);
		List<StringBuffer> sbs =  new ArrayList<StringBuffer>();
		for(int i = 0; i < contentList.size();i++){
			sbs.add(new StringBuffer());
		}
		for(int i = 0; i < OLD_SMS.size(); i++){
			if(i == 4){// <1>?</1><2>?</2><3>?</3>
				String contentMsg = getValue(i);
				if(contentList.size() == 1) {
					sbs.get(0).append(StringUtils.replace(OLD_SMS.get(i), "?",contentMsg));
				} else {
					for (int j = 0; j < contentList.size(); j++) {
						String baseStr1 = "<1>?</1>";
						String baseStr2 = "<2>?</2>";
						String baseStr3 = "<3>?</3>";
						StringBuffer ssb = new StringBuffer();
						ssb.append(StringUtils.replace(baseStr1, "?", contentList.size() + ""))
								.append(StringUtils.replace(baseStr2, "?",(j + 1) + ""))
								.append(StringUtils.replace(baseStr3, "?",contentList.get(j)));
						sbs.get(j).append(StringUtils.replace(OLD_SMS.get(i), "?",ssb.toString()));
					}
				}
			} else {
				addAppendAllList(sbs,(StringUtils.replace(OLD_SMS.get(i),"?",getValue(i))));
			}
		}
		
		addAppendAllList(sbs,"<endl/>");
		List<String> result = new ArrayList<String>();
		for(int i = 0; i < sbs.size();i++){
			result.add(sbs.get(i).toString());
		}
		
		return result;
	}


	//"Srv_node","OrgSerialNo","SendPort","User","Smcont","SmType","RecvModuleName"
	public static MtOldSms parse(List<String> list) {
		MtOldSms sms = null;
		
		if(list != null && list.size() >= 7){
			sms = new MtOldSms();
			sms.setClientName(list.get(0));
			sms.setSerialNo(list.get(1));
			sms.setSendPort(list.get(2));
			sms.setMobileNo(list.get(3));
			sms.setContent(list.get(4));
			sms.setMesgType(StringUtils.toInteger(list.get(5)));			
		}
		return sms;
	}
	
	public static void main(String[] args){
		String str = "故障停电通知：尊敬的张奕初用户，2018-07-17 23:31:08新明村委会湿更村所在的电力线路部分用户因线路出现故障停电，正在夺";
		str = str.substring(0,5);
		System.out.println(str.length());
	}
	
}
